function FlogSaveStockPlanet () {
    var server = top.FLogGetServer ();
    var facility = server.GetPlanet (window.userloc, "stock");
    facility["sector"] = POgetSector ();
    
    var amount = window.amount;
    var amount_max = window.amount_max;
    var amount_min = window.amount_min;
    var amount_ship = window.amount_ship;
    var player_sell = window.player_sell_price;
    var player_buy = window.player_buy_price;
    
    facility["credits"] = window.planet_credits;
    facility["stock"] = {};
    for (var commodity in amount_ship) {
        if (amount_ship.hasOwnProperty(commodity)) {
            var st = [];
            st.push (player_sell[commodity]);
            st.push (amount[commodity]);
            st.push (amount_min[commodity]);
            st.push (amount_max[commodity]);
            st.push (player_buy[commodity]);
            facility["stock"][commodity] = st;
        }
    }
    server.SaveFacility (window.userloc, facility, "stock");
    
    facility = server.GetPlanet (window.userloc, "facility");
    facility["sector"] = POgetSector ();
    var coords = POgetCoords ();
    coords[0] = "(";
    coords[coords.length-1] = ")";
    facility["coords"] = coords;
    facility["player_owned"] = "false";

    var link = document.getElementsByTagName ("h1")[0].getElementsByTagName("a")[0];
    facility["name"] = link.textContent;
    var imgs = link.parentNode.getElementsByTagName ("img")[0].getAttribute ("src").split("/");
    facility["planet_class"] = imgs[imgs.length-1].split(".")[0].split("_")[1].toUpperCase();
    
    if (!facility["faction"]) facility["faction"] = "???";
    if (!facility["population"]) facility["population"] = "???";
    facility["trade_open"] = "true";
    server.SaveFacility (window.userloc, facility, "facility");
}

function FlogSaveStockStarbase () {
    var server = top.FLogGetServer ();
    var facility = server.GetStarbase (window.userloc, "stock");
    facility["sector"] = POgetSector ();
    
    var amount = window.amount;
    var amount_max = window.amount_max;
    var amount_min = window.amount_min;
    var amount_ship = window.amount_ship;
    var player_sell = window.player_sell_price;
    var player_buy = window.player_buy_price;
    
    facility["credits"] = window.starbase_credits;
    facility["stock"] = {};
    var inputs = document.getElementsByTagName ("input");
    var transfer = null;
    for (var i=0; i<inputs.length; i++) {
        if (inputs[i].getAttribute ("value") == "<- Transfer ->") {
            transfer = inputs[i];
            break;
        }
    }
    if (transfer) {
        var tbl = transfer.parentNode.parentNode.cells[2].firstChild;
        var row = tbl.rows[tbl.rows.length-2];
        facility["space"] = row.cells[1].textContent;
    }
    
    for (var commodity in amount_ship) {
        if (amount_ship.hasOwnProperty(commodity)) {
            var st = [];
            st.push (player_sell[commodity]);
            st.push (amount[commodity]);
            st.push (amount_min[commodity]);
            st.push (amount_max[commodity]);
            st.push (player_buy[commodity]);
            facility["stock"][commodity] = st;
        }
    }
    server.SaveFacility (window.userloc, facility, "stock");
    
    facility = server.GetStarbase (window.userloc, "facility");
    facility["sector"] = POgetSector ();
    var coords = POgetCoords ();
    coords[0] = "(";
    coords[coords.length-1] = ")";
    facility["coords"] = coords;
    facility["player_owned"] = "false";
    if (!facility["owner"]) facility["owner"] = "???";
    
    var link = document.getElementsByTagName ("h1")[0].getElementsByTagName("a")[0];
    facility["name"] = link.textContent;
    var imgs = link.parentNode.getElementsByTagName ("img")[0].getAttribute ("src").split("/");
    var img = imgs[imgs.length-1].split(".")[0];
    var parts = img.split("_");
    if (parts[1][0] == 'p') {
        facility["player_owned"] = "true";
    }
    var faction = parts[1][1];
    if (faction == '0') {
        facility["faction"] = "Neutral";
    } else if (faction == '1') {
        facility["faction"] = "Federation";
    } else if (faction == '2') {
        facility["faction"] = "Empire";
    } else if (faction == '3') {
        facility["faction"] = "Union";
    }
    facility["rings"] = Number (parts[2][1]);
    
    if (!facility["population"]) {
        if (facility["rings"] == 1) facility["population"] = "500 - 5,000";
        else if (facility["rings"] == 2) facility["population"] = "5,000 - 15,000";
        else if (facility["rings"] == 1) facility["population"] = "15,000 - 30,000";
        else facility["population"] = ">30,000"
    }
    if (!facility["owner"]) {
        facility["owner"] = "???";
        facility["owner_alliance"] = "???";
    }
    facility["trade_open"] = "true";
    server.SaveFacility (window.userloc, facility, "facility");
}

function FlogSaveStockBuilding () {
    var server = top.FLogGetServer ();
    var facility = server.GetBuilding (window.userloc, "stock");
    facility["sector"] = POgetSector ();
    
    var amount = window.amount;
    var amount_max = window.amount_max;
    var amount_min = window.amount_min;
    var amount_ship = window.amount_ship;
    var player_sell = window.player_sell_price;
    var player_buy = window.player_buy_price;
    
    facility["credits"] = window.building_credits;
    facility["space"] = window.base_space;
    facility["stock"] = {};
    for (var commodity in amount_ship) {
        if (amount_ship.hasOwnProperty(commodity)) {
            var st = [];
            st.push (player_sell[commodity]);
            st.push (amount[commodity]);
            st.push (amount_min[commodity]);
            st.push (amount_max[commodity]);
            st.push (player_buy[commodity]);
            facility["stock"][commodity] = st;
        }
    }
    server.SaveFacility (window.userloc, facility, "stock");

    facility = server.GetBuilding (window.userloc, "facility");
    facility["sector"] = POgetSector ();
    var coords = POgetCoords ();
    coords[0] = "(";
    coords[coords.length-1] = ")";
    facility["coords"] = coords;
    facility["player_owned"] = "true";
        
    var link = document.getElementsByTagName ("h1")[0].getElementsByTagName("a")[0];
    facility["building_type"] = link.textContent;
    server.SaveFacility (window.userloc, facility, "facility");
    
    var inps = document.getElementsByTagName ("input");
    for (var i=0; i<inps.length; i++) {
        if (inps[i].getAttribute("value") == "<- Transfer ->") {
            var name = inps[i].parentNode.parentNode.previousSibling.cells[2].textContent.split("'")[0];
            facility["owner"] = name;
            break;
        }
    }
    if (!facility["owner_alliance"]) facility["owner_alliance"] = "???";
    if (!facility["faction"]) facility["faction"] = "???";
    if (!facility["building_condition"]) facility["building_condition"] = "???";
    facility["trade_open"] = "true";
    server.SaveFacility (window.userloc, facility, "facility");
}

function FlogSaveStock () {
    if (document.URL.indexOf ("building_trade") != -1) {
        FlogSaveStockBuilding ();
    } else if (document.URL.indexOf ("planet_trade") != -1) {
        FlogSaveStockPlanet ();
    } else {
        FlogSaveStockStarbase ();
    }
    
}

FlogSaveStock();
